<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/29
 * Time: 17:58
 */

namespace app\common\lib;
use app\common\lib\Aes;
class IAuth
{
    //加密
    public static function setToken($data = array()){
        //按字段排序
        ksort($data);
        //拼接字符串数据
        $string = http_build_query($data);
        //加密
        $token = Aes::encrypt($string,config('app.aeskey'));
        //返回加密的字符串
        return $token;
    }

    //校验token的合法性
    public static function checkTokenPass($token = ''){
        //解密
        $str = Aes::decrypt($token,config('app.aeskey'));

        //判定解密后的字符串是否存在
        if(empty($str)){
            return false;
        }

        //将字符串转化成数组
        parse_str($str,$arr);

        if(!$arr || !is_array($arr)){
            return false;
        }

        //判定时间
        if((time() - ceil($arr['time'] / 1000)) > 10000){
            return false;
        }
        return $arr;
    }

    //获取13位的时间戳
    public static function get13Time(){
        list($t1,$t2) = explode(' ',microtime());
        return $t2 . ceil($t1 * 1000);
    }
}